package gui.listeners;

import gfx.hdr.ImageHDR;
import gfx.layers.Layer;
import gfx.layers.LayerManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import core.exceptions.ImageDimensionException;

public class MainWindowButtonListener implements ActionListener{
	
	public static final String CREATE_HDR = "createHdr";
	private LayerManager layerManager;
	
	public MainWindowButtonListener(LayerManager layerManager){
		this.layerManager = layerManager;
	}

	@Override
  public void actionPerformed(ActionEvent e) {
	  if(e.getActionCommand().equals(CREATE_HDR)){
	  	ImageHDR iHDR = createHDRFromSelectedLayers();
	  	if(iHDR != null){
	  		System.out.println("Creating HDR...");
	  		Thread calc = new Thread(iHDR);
	  		calc.start();
	  	}
	  }
  }

	private ImageHDR createHDRFromSelectedLayers(){
		ImageHDR iHDR = null;
  	for(int i = 0; i < layerManager.getSize(); i++){
  		if(((Layer)layerManager.getElementAt(i)).isSelected()){
  			Layer l = (Layer)layerManager.getElementAt(i);
  			if(iHDR == null)
					iHDR = new ImageHDR(l.getImage().getWidth(), l.getImage().getHeight(), layerManager);
				
		    try {
          iHDR.addExposure(l);
        }
		    catch (ImageDimensionException e1) {
          e1.printStackTrace();
        }
  		}
  	}
  	return iHDR;
	}
}
